package org.lanqiao.tjut.control;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.lanqiao.tjut.bean.BalanceFormBean;
import org.lanqiao.tjut.bean.PayFormBean;
import org.lanqiao.tjut.bean.UserBean;
import org.lanqiao.tjut.myUtils.MD5Util;
import org.lanqiao.tjut.seriver.BalanceService;
import org.lanqiao.tjut.seriver.ElectService;
import org.lanqiao.tjut.seriver.OpeService;
import org.lanqiao.tjut.seriver.PayService;
import org.lanqiao.tjut.seriver.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class PaymentControl {
	@Resource
	private BalanceService balanceService;
	@Resource
	private PayService payService;
	@Resource
	private ElectService electService;
	@Resource
	private UserService UserService;
	@Resource
	private OpeService OpeService;

	// 操作员进行扣费处理
	@RequestMapping(value = "/doDeductElectricity.do")
	public String doDeductElectricity(BalanceFormBean balanceBean, HttpSession session) {
		// 显示所有用户的余额信息（查询余额表）
		List<BalanceFormBean> lstbaBean = balanceService.selectAllUsersBalance();
		session.setAttribute("allUserBalance", lstbaBean);
		return "payforManager/DeductElectricity";
	}

	// 操作员为用户进行充值
	@RequestMapping(value = "/doRecharge0.do")
	public String doRecharge0(HttpServletRequest request, HttpSession session) {
		String opeName = request.getParameter("opeName");
		session.setAttribute("opeName", opeName);
		return "payforManager/Recharge";
	}

	// 操作员为用户进行充值
	@RequestMapping(value = "/doRecharge.do")
	public String doRecharge(String opreatorName, UserBean userB, HttpSession session) {
		String userAccount = userB.getUserAccount();
		UserBean userB2 = UserService.selectByUserAccount(userAccount);
		if (userB2 == null) {
			session.setAttribute("txtError", "账号不存在！");
			return "payforManager/Recharge";
		} else if (MD5Util.MD5(userB.getUserPsw()).equals(userB2.getUserPsw())) {
			session.setAttribute("userB", userB2);
			session.setAttribute("opeName0", opreatorName);
			return "payforManager/rechargePage";
		} else {
			session.setAttribute("txtError", "账号或者密码输入错误，请重新输入！");
			return "payforManager/Recharge";
		}
	}

	// 充值后--充值成功显示该账户查询界面--充值失败返回本页面提示充值失败
	@RequestMapping(value = "/getRecharge.do")
	public String getRecharge(PayFormBean payBean, UserBean userB, HttpSession session, HttpServletResponse response) {
		// 1.在充值表中新增充值记录
		// 获取当前时间
		Date date = new Date();
		// 设置时间格式
		SimpleDateFormat dateF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String payDate = dateF.format(date);
		payBean.setPayPaydate(payDate);
		// 调用数据插入方法
		int i = payService.insertRechargeRecord(payBean);
		if (i > 0) {
			// 2.在余额表中更新最新余额
			// 查询充值前余额表中该用户的余额
			BalanceFormBean balanceB = balanceService.selectMyBalance(userB.getUserAccount());
			Integer oldBalance = balanceB.getBalaBalamoney();
			// 获取用户当前充值的余额
			Integer newBalance = payBean.getPayPaymoney();
			// 将两者相加
			Integer sumBalance = oldBalance + newBalance;
			// 更新余额表中该用户的余额
			BalanceFormBean balanceB2 = new BalanceFormBean();
			balanceB2.setBalaBalamoney(sumBalance);
			balanceB2.setUserAccount(userB.getUserAccount());
			// 更新余额表余额
			balanceService.updateUserBalance(balanceB2);
			balanceB2.setBalaCode(0);
			// 更新余额表标志位
			balanceService.updateUserBalanceCode(balanceB2);
			// // 跳转到该操作员主界面(重新设置操作员信息缓存)，可以设置提示框提示充值成功
			// OpeBean opeB =
			// OpeService.selectByOpeName(payBean.getOpreatorName());
			// session.setAttribute("UserNameO", opeB);
			
			return "commons/menuO";
		} else {
			session.setAttribute("Errorss", "对不起！充值失败");
			return "payforManager/rechargePage";
		}

	}
}
